package service;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import model.ReagentRenewal;
import model.ReagentRenewalGroup;
import model.Staff;
import model.Station;

public class ReagentRenewalService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " trr.id,trr.createTime,trr.instrumentName,trr.reagentName,trr.potency,trr.deadline,trr.creator,ts.name,tc.name AS companyName1,tsr.name AS servicerName,trr.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_reagent_renewal trr ") ;
	    pageSql.join(" t_station ts " , " ts.id = trr.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = trr.serviserId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("trr.createTime","trr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("trr.createTime","trr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("trr.stationId") ; //点位	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" trr.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " trrg.id , trrg.createTime , trrg.equipmentName , ts.name AS servicerName , trrg.recorder  " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_reagent_renewal_group trrg ") ;
	    pageSql.join(" t_servicer ts ", " ts.id = trrg.servicerId ");
		pageSql.setEqualCondition("trrg.stationId") ; //点位
		pageSql.setNotLessThanCondition("trrg.createTime", "startTime");
		pageSql.setNotMoreThanCondition("trrg.createTime", "endTime");
	    pageSql.orderBy(" trrg.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			Long groupId = record.getLong("id");
			List<Record> records = ReagentRenewal.dao.findByGroupId(groupId);
			record.set("list", records);
		}
		return page ;
	}
	

	public Ret save( List<ReagentRenewal> list , String name , Staff staff , Station station) {
		ReagentRenewalGroup group = new ReagentRenewalGroup();
		group.setStationId(station.getId());
		group.setServicerId(staff.getServicerId());
		group.setRecorder(staff.getName());
		group.setEquipmentName(name);
		group.save();
		for (ReagentRenewal reagentRenewal : list) {
			reagentRenewal.setInstrumentName(name);
			reagentRenewal.setGroupId(group.getId());
			reagentRenewal.setServiserId(staff.getServicerId());
			reagentRenewal.setStationId(station.getId());
			reagentRenewal.setRecorder(staff.getName());
			reagentRenewal.save();
		}
	    return ok() ;
	}
}
